﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Interactive.Utility;
using TravelLink.DAL;

namespace TravelLink.WebModel {
    public class UserStoreFacade {
        //    static IUserStore _sessionStore = Singleton<TestUserStore>.Instance;
         static IUserStore _sessionStore = Singleton<SessionUserStore>.Instance;

        public static void Login(OnlineUserInfo user) {
            user.LoginTime = DateTime.Now;
            _sessionStore.Login(user);
        }

        public static void Update(OnlineUserInfo user) {
            _sessionStore.Login(user);
        }

        public static void Logout() {
            _sessionStore.Logout();
        }

        public static OnlineUserInfo Get() {
            return _sessionStore.Get();
        }
    }

    public interface IUserStore {
        void Login(OnlineUserInfo user);
        void Logout();
        OnlineUserInfo Get();
    }

    public class SessionUserStore : IUserStore {

        private string SESSION_KEY = "TDL_SessionUserStore";
        public SessionUserStore() {

        }

        public void Login(OnlineUserInfo user) {
            HttpContext.Current.Session[SESSION_KEY] = user;

            SetCookie(user.DBUser.Email, user.DBUser.Password);
        }

        private void SetCookie(string user, string pwd) {
            var cookie = HttpContext.Current.Request.Cookies[SESSION_KEY];
            bool isSet = cookie != null;
            if (cookie == null) {
                cookie = new HttpCookie(SESSION_KEY);
                cookie.Path = "/";
            }

            cookie.Values["User"] = user;
            cookie.Values["Pwd"] = pwd;

            if (isSet) {
                HttpContext.Current.Response.SetCookie(cookie);
            }
            else {
                HttpContext.Current.Response.AppendCookie(cookie);
            }
        }

        public void Logout() {
            HttpContext.Current.Session.Remove(SESSION_KEY);
            var cookie = HttpContext.Current.Request.Cookies[SESSION_KEY];
            if (cookie != null) {
                cookie.Expires = DateTime.Now.AddDays(-1);
                HttpContext.Current.Response.SetCookie(cookie);
            }

        }

        public OnlineUserInfo Get() {
            var onlineInfo = HttpContext.Current.Session[SESSION_KEY] as OnlineUserInfo;
            if (onlineInfo == null) {
                var cookie = HttpContext.Current.Request.Cookies[SESSION_KEY];
                if (cookie != null) {
                    string userName = cookie["User"];
                    string pwd = cookie["Pwd"];
                    if (UserController.GetInstance().Login(userName, pwd) == "OK") {
                        return HttpContext.Current.Session[SESSION_KEY] as OnlineUserInfo;
                    }
                }
            }
            return onlineInfo;
        }
    }

    public class TestUserStore : IUserStore {
        public TestUserStore() {

        }

        public void Update(TL_User userInfo) {

        }

        public void Login(OnlineUserInfo user) {

        }

        public void Logout() {

        }

        public OnlineUserInfo Get() {
            var entity = DataContextFactory.Create().TL_User.Where(u => u.UserCode == new Guid("246A5508-B447-4513-9E9F-6181FFF00CDE")).FirstOrDefault();
            return new OnlineUserInfo {
                DBUser = entity,
                LoginIP = "",
                LoginTime = DateTime.Now
            };

        }
    }
}
